package com.dao.impl;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dao.HeadImageDAO;
import com.model.Article;
import com.model.HeadImage;

@Repository
public class HeadImageDAOImpl  implements HeadImageDAO{

	@Autowired
	private SessionFactory sessionFactory;
	
	public int write(HeadImage image) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(image);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return image.getId();
	}

	public HeadImage read(String path) {
		// TODO Auto-generated method stub
		Session s = sessionFactory.getCurrentSession();
		String hql = "select img From HeadImage img where img.path=:path";
		
		Query query = s.createQuery(hql);
		query.setString("path", path);

		List<HeadImage> list = (List<HeadImage>) query.list();
		if(list.size()>0){
			return (HeadImage) list.get(0);
		}else{
			return null;
		}
	}

	public HeadImage read(int id) {
		// TODO Auto-generated method stub
		HeadImage image = (HeadImage) sessionFactory.getCurrentSession().get(HeadImage.class, id);
		return image;
	}

	public int delete(String path) {
		// TODO Auto-generated method stub
		Session s = sessionFactory.getCurrentSession();
		String hql = "delete from head_image where path=:path";
		Query query = s.createSQLQuery(hql);
		query.setString("path", path);
		return query.executeUpdate();
	}

}
